package net.microfigure.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.commons.shiro.ShiroUser;
import net.microfigure.model.Family;
import net.microfigure.service.IAreaInfoService;
import net.microfigure.service.IFamilyService;
import net.microfigure.service.impl.AreaInfoServiceImpl;
import net.microfigure.commons.base.BaseController;

/**
 * <p>
 * 家庭表 前端控制器
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-15
 */
@Controller
@RequestMapping("/family")
public class FamilyController extends BaseController {

	@Autowired 
	private IFamilyService familyService;
	@Autowired
	private IAreaInfoService areaInfoService;

	@GetMapping("/manager")
	public String manager() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); 
		String username = user.getName();
		System.out.println("当前登录的shiro用户名"+username);
		return "admin/family/familyList";
	}


	@PostMapping("/dataGrid")
	@ResponseBody
	public Object dataGrid(Integer page,Integer rows,String sort, String order) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); 
		String name = user.getName();
		System.out.println("当前登录的shiro用户名"+name);
		familyService.selectDataGrid(pageInfo,name);
		return pageInfo;
	}


	/**
	 * 添加页面
	 * @return
	 */
	@GetMapping("/addPage")
	public String addPage() {
		return "admin/family/familyAdd";
	}

	/**
	 * 添加
	 * @param 
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public Object add(@Valid Family family) {

		System.out.println(family.getAreaNo());
		String familyAreaNo = areaInfoService.selectcCodeById(family.getAreaNo());
		System.out.println("familyAreaNo=="+familyAreaNo);
		family.setAreaNo(familyAreaNo);
		System.out.println("family.getFamilyNo()=="+family.getFamilyNo());
		boolean b = familyService.insert(family);
		if (b) {
			return renderSuccess("添加成功！");
		} else {
			return renderError("添加失败！");
		}
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		System.out.println("删除的当前为sid==="+id);
		boolean b=	familyService.deleteById(id);
		if (b) {
			return renderSuccess("删除成功！");
		} else {
			return renderError("删除失败！");
		}

	}

	/**
	 * 编辑
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/editPage")
	public String editPage(Model model, Long id) {
		Family family = familyService.selectById(id);
		model.addAttribute("family", family);
		return "admin/family/familyEdit";
	}

	/**
	 * 编辑
	 * @param 
	 * @return
	 */
	@PostMapping("/edit")
	@ResponseBody
	public Object edit(@Valid Family family) {

		System.out.println("打印family"+family.getAreaNo());
		String areaNo = areaInfoService.selectAreaNoById(family.getAreaNo());
		System.out.println("查出真正的code"+areaNo);
		family.setAreaNo(areaNo);
		boolean b = familyService.updateById(family);
		if (b) {
			return renderSuccess("编辑成功！");
		} else {
			return renderError("编辑失败！");
		}
	}
}
